package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import newsDB.NewsDao;
import newsDB.TopicDao;
import newsEntity.Page;

public class IndexServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1058149248533247547L;

	/**
	 * Constructor of the object.
	 */
	public IndexServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setAttribute("topicList", TopicDao.td.findTopic());
		Long ntid = null;
		int pageNum = 1;
		Page page = null;
		try {
			ntid = Long.parseLong(request.getParameter("ntid"));
		} catch (Exception e) {
		}
		try {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		} catch (Exception e) {
		}
		if (ntid != null) {
			page = NewsDao.nd.newsForTopic(ntid, pageNum);
		} else {
			page = NewsDao.nd.findForPageNum(pageNum);
		}
		request.setAttribute("ntid", ntid);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
